var itemID;
var token;
var favoriteIcon = document.getElementById("favoriteIcon");
var pageflag;
var keyWords = "";
var title = "";
var insertDiv = document.getElementById("insertDiv");

(function($) {

	mui.init({});

	setPxPerRem();
	mui.plusReady(function() {
		itemID = getQueryString("itemId");
		pageflag = getQueryString("pageflag");
		keyWords = getQueryString("keyWords");
		title = getQueryString("titleStr");
		token = localStorage.getItem("token_type") + " " + localStorage.getItem("access_token");
		if(isHaveNet()) {
			plus.nativeUI.showWaiting();

			if(pageflag == "page_project.html") {
				if(isEmpty(title)) {
					titleText = "招商项目";
				}
				document.getElementById("titleText").innerHTML = title;
				if(!isEmpty(token)) {
					getAllProject(token, itemID, keyWords);
				} else {
					mui.toast("请登陆后查看");
				}
			}
		} else {
			mui.toast("网络连接失败，请连网后重试")
		}
	});

})();

function getAllProject(authorization, id, keyWords) {
	var settings = {
		url: getRequestAddressUrl("testline") + "/user/zs_project",
		method: "GET",
		data: {
			page: 1,
			per_page: 20,
			sort: "updated_at",
			order: "DESC",
			keywords: keyWords,
			zs_project_category_id: id
		},
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}

	$.ajax(settings).done(function(data) {
		plus.nativeUI.closeWaiting();
		console.log(JSON.stringify(data));

		if(!isEmpty(data)) {
			var rows = data.data;
			if(!isEmpty(rows)) {
				
				var isOnly = false ; 
				if(rows.length == 1){
					isOnly = true;
				}
				
				for(var i = 0; i < rows.length; i++) {
					createProjectItem(rows[i] , i , isOnly);
				}
			}
		}
	});
}

function createProjectItem(row , index , isOnly) {
	if(!isEmpty(row)) {

		var contentStr = row.content;

		var reg = new RegExp("@@", "g");
		contentStr = contentStr.replace(reg, "：")
		console.log("contentStr===>>" + contentStr);

		var divArr = contentStr.split("||");
		var infoStr = "";
		for(var i = 0; i < divArr.length; i++) {
			infoStr += '<div>' + divArr[i] + '</div>'
		}
		var infoDivStr = '<img src="../../ui/building/images/07project/down.png" /></div><div class="mui-hidden project-info" >' + infoStr + '</div>';
		if(index == 0){
			if(isOnly){
				infoDivStr = '<img src="" /></div><div class="project-info" >' + infoStr + '</div>';
			} else {
				infoDivStr = '<img src="../../ui/building/images/07project/close.png" /></div><div class="project-info" >' + infoStr + '</div>';
			}
			
		}
		
		var createLi = document.createElement("li");
		createLi.id = row.id;
		createLi.className = "mui-table-view-cell";
		createLi.innerHTML = '<div><p>' + row.name + '</p>' + infoDivStr;
		document.body.appendChild(createLi);
		insertBefore(createLi , insertDiv);
	}
}

mui("#projectList").on('tap', 'li', function() {
	var infoDiv = this.getElementsByTagName("div")[1];
	var infoImg = this.getElementsByTagName("img")[0];
	
	if(isEmpty(infoImg.src)){
		return;
	}
	
	if(infoDiv.classList.contains("mui-hidden")) {
		showEle(infoDiv);
		infoImg.src = "../../ui/building/images/07project/close.png";
	} else {
		hiddenEle(infoDiv);
		infoImg.src = "../../ui/building/images/07project/down.png";
	}

});